const { response } = require("express")
const { UserModel } = require("../model")
//用户注册
exports.register = async (req, res, next) => {
  try {
    //创建文档对象
    let user = new UserModel(req.body)
    //保存文档对象到数据库
    await user.save()
    // 转成json 
    user = user.toJSON()
    //删除用户密码属性
    delete user.password
    //发送成功响应，返回用户信息
    res.send(user)
  } catch (err) {
    next(err)
  }
}
//用户修改
exports.update = async (req, res, next) => {
  try {
    await UserModel.updateOne({ _id: req.params.id }, { $set: req.body })
      .then(response => {
        res.send({ message: '修改成功' })
      })
      .catch(err => {
        next(err)
      })
  } catch (err) {
    next(err)
  }
}